package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.common.util.utils.HttpClientUtils;
import com.atguigu.guli.common.util.utils.StreamUtils;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.handler.JwtHelper;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.entity.PayLog;
import com.atguigu.guli.service.trade.feign.EduFeignClient;
import com.atguigu.guli.service.trade.service.OrderService;
import com.atguigu.guli.service.trade.service.PayLogService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-07
 */
@RestController
@RequestMapping("/api/trade/order")
public class ApiOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    PayLogService payLogService;
    @Autowired
    EduFeignClient eduFeignClient;
    //6、用户查询自己是否购买过某个课程
    @GetMapping("auth/isbuy/{courseId}")
    public R isbuy(@PathVariable String courseId , HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getCourseId , courseId);
        queryWrapper.eq(Order::getMemberId , memberId);
        queryWrapper.eq(Order::getStatus , 1);
        int count = orderService.count(queryWrapper);
        return R.ok().setData("isBuy" , count>0);//true表示已购买
    }
    //5、用户查询订单支付状态
    @GetMapping("auth/get-paystatus/{orderId}")
    public R getPayStatus(@PathVariable String orderId){
        Order order = orderService.getById(orderId);
        return R.ok().setData("status" , order.getStatus());//1已支付
    }
    //4、用户支付成功后微信回调的接口
    @PostMapping("callback")
    public String callback(HttpServletRequest request){
        String xmlStr = orderService.callback(request);
        return xmlStr;
    }
    //3、用户在订单详情页点击支付按钮返回支付二维码
    @GetMapping("auth/toPay/{orderId}")
    public R toPay(@PathVariable String orderId ,HttpServletRequest request){
        Map data = orderService.toPay(orderId , request);
        return R.ok().setData(data);
    }

    //2、根据订单id查询订单详情
    @GetMapping("auth/getOrder/{id}")
    public R getOrder(@PathVariable String id){
        Order order = orderService.getById(id);
        return R.ok().setData("item" , order);
    }

    //1、创建待支付的订单
    @PostMapping("auth/create-order/{courseId}")
    public R createOrder(HttpServletRequest request , @PathVariable String courseId){
        //获取用户信息
        String memberId = JwtHelper.getId(request);
        String orderId = orderService.createOrder(memberId , courseId);
        return R.ok().setData("id" , orderId); //创建订单后返回订单id
    }

}

